﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_Childrens : UserControl
    {
        private IChildrenBLL BLL = new ChildrenBLL();
        private IHisSpouseBLL spBLL = new HisSpouseBLL();
        private IMasEducationDegreeBLL degreeBLL = new MasEducationDegreeBLL();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
        private IMasGenderBLL genderBLL = new MasGenderBLL();
        private IMasNamePrefixBLL npBLL = new MasNamePrefixBLL();
        private ComboboxUtilites comboboxUtilites = new ComboboxUtilites();
        private SsMng ssMng = new SsMng();
		private const int ItemsPerRequest = 10;
		private Main main = new Main();

        public PS_Childrens()
        {
            DataItem = null;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            lblEmpSeq.Text = Session["emp_seq"].ToString();
            if (!IsUserControlPostBack)
            {
                ViewState.Add("IsUserControlPostBack", true);
                cmbSpouses.Items.Clear();
                BindGenderRadio();

                if (DataItem != null)
                {
                    ListSpouseComboBox(lblEmpSeq.Text.Trim());

                    if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                    {
                        lblSequence.Text = DataBinder.Eval(DataItem, "CHILD_SEQ").ToString();

                        txtNo.Text = DataBinder.Eval(DataItem, "CHILD_NO").ToString();
                        if (DataBinder.Eval(DataItem, "SPOUSE_SEQ") != null && DataBinder.Eval(DataItem, "SPOUSE_SEQ").ToString() != "0")
                            cmbSpouses.SelectedValue = DataBinder.Eval(DataItem, "SPOUSE_SEQ").ToString();
                        
                        rdoGender.SelectedValue = DataBinder.Eval(DataItem, "GENDER_SEQ").ToString();
                        cmbNamePrefixes.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "PREFIX_NAME").ToString(), DataBinder.Eval(DataItem, "NAME_PREFIX_SEQ").ToString()));
                        cmbNamePrefixes.SelectedValue = DataBinder.Eval(DataItem, "NAME_PREFIX_SEQ").ToString();
                        txtName.Text = DataBinder.Eval(DataItem, "CHILD_NAME").ToString();
                        txtLastName.Text = DataBinder.Eval(DataItem, "CHILD_LAST_NAME").ToString();
                        txtBirthDate.SetValue(DataBinder.Eval(DataItem, "BIRTH_DATE", "{0:dd/MM/yyyy}").ToString());
                        txtCitizen.Text = DataBinder.Eval(DataItem, "CITIZEN").ToString();
                        cmbStatus.SelectedValue = DataBinder.Eval(DataItem, "CHILD_STATUS").ToString();

                        txtDeathDate.SetValue(DataBinder.Eval(DataItem, "DEATH_DATE", "{0:dd/MM/yyyy}").ToString());
                        txtDeathRefNo.Text = DataBinder.Eval(DataItem, "DEATH_REF_NO").ToString();

                        chkEduFee.Checked = DataBinder.Eval(DataItem, "FEE_EDU").ToString() == "1";
                        bool eduStatus = DataBinder.Eval(DataItem, "EDU_STATUS").ToString() == "1";
                        chkEduStatus.Checked = eduStatus;
                        ChangeEducationStatus(eduStatus);
                        cmbEduDegree.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_DEGREE_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_DEGREE_SEQ").ToString()));
                        cmbEduDegree.SelectedValue = DataBinder.Eval(DataItem, "EDU_DEGREE_SEQ").ToString();

                        cmbInstitutes.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EDU_INS_NAME").ToString(), DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString()));
                        cmbInstitutes.SelectedValue = DataBinder.Eval(DataItem, "EDU_INS_SEQ").ToString();
                        cmbChildType.SelectedValue = DataBinder.Eval(DataItem, "CHILD_TYPE").ToString();

                        CheckVisibleControl();
                    }
                }
            }
        }

        private void BindGenderRadio()
        {
            IList<MasGenderInfo> InfoList = genderBLL.ListMasGenders();

            rdoGender.DataSource = InfoList;
            rdoGender.DataBind();
        }
        
        public void ListSpouseComboBox(string Employee)
        {
            Nullable<int> nEmpSeq = Employee.Length > 0 ? Int32.Parse(Employee) : (int?)null;
            DataSet ds = null;
            DataTable dt = null;

            ds = spBLL.LoadSpouse((int?)null, nEmpSeq);
            dt = ds.Tables[0];
            cmbSpouses.DataSource = dt;
            cmbSpouses.DataBind();
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            BLL.Insert(lblEmpSeq.Text.Trim()
                       , txtNo.Text.Length > 0 ? Int32.Parse(txtNo.Text) : 0
                       , rdoGender.SelectedValue
                       , cmbNamePrefixes.SelectedValue
                       , txtName.Text.Trim()
                       , txtLastName.Text.Trim()
                       ,
                       txtBirthDate.GetStringValue().Length > 0
                           ? DateTime.Parse(txtBirthDate.GetStringValue())
                           : (DateTime?) null
                       , cmbStatus.SelectedValue
                       ,
                       txtDeathDate.GetStringValue().Length > 0
                           ? DateTime.Parse(txtDeathDate.GetStringValue())
                           : (DateTime?) null
                       , txtDeathRefNo.Text.Trim()
                       , chkEduStatus.Checked ? "1" : "0"
                       , (DateTime?) null
                       , chkEduFee.Checked
                       , cmbSpouses.SelectedValue
                       , cmbEduDegree.SelectedValue
                       , cmbInstitutes.SelectedValue
                       , cmbChildType.SelectedValue
                       , txtCitizen.Text.Trim()
					   , ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            BLL.Update(Int32.Parse(lblSequence.Text.Trim())
                , lblEmpSeq.Text.Trim()
                , txtNo.Text.Length > 0 ? Int32.Parse(txtNo.Text) : 0
                , rdoGender.SelectedValue
                , cmbNamePrefixes.SelectedValue
                , txtName.Text.Trim()
                , txtLastName.Text.Trim()
                , txtBirthDate.GetStringValue().Length > 0 ? DateTime.Parse(txtBirthDate.GetStringValue()) : (DateTime?)null
                , cmbStatus.SelectedValue
                , txtDeathDate.GetStringValue().Length > 0 ? DateTime.Parse(txtDeathDate.GetStringValue()) : (DateTime?)null
                , txtDeathRefNo.Text.Trim()
                , chkEduStatus.Checked ? "1" : "0"
                , (DateTime?)null
                , chkEduFee.Checked
                , cmbSpouses.SelectedValue
                , cmbEduDegree.SelectedValue
                , cmbInstitutes.SelectedValue
                , cmbChildType.SelectedValue
                , txtCitizen.Text.Trim()
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }


        protected void chkEduStatus_CheckedChanged(object sender, EventArgs e)
        {
            ChangeEducationStatus(chkEduStatus.Checked);
        }

        private void ChangeEducationStatus(bool value)
        {
            chkEduFee.Enabled = value;
            if (!value)
            {
                chkEduFee.Checked = false;
            }
        }

        protected void CheckVisibleControl()
        {
            bool bVisible = cmbStatus.SelectedValue == "2"; //สถานะเสียชีวิต

            lblDeathDate.Visible = bVisible;
            txtDeathDate.Visible = bVisible;
            lblDeathRefNo.Visible = bVisible;
            txtDeathRefNo.Visible = bVisible;
        }

        protected void cmbStatus_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            CheckVisibleControl();
        }

        protected void cmbNamePrefixes_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            //cmbNamePrefixes.Items.Clear();
            Nullable<int> GenderSeq = string.IsNullOrEmpty(rdoGender.SelectedValue) ? (int?)null : Int32.Parse(rdoGender.SelectedValue);

            DataSet ds = null;
            DataTable dt = null;

            ds = npBLL.LoadNamePrefixes((int?)null, GenderSeq);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            //int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbNamePrefixes.Items.Add(new RadComboBoxItem(dt.Rows[i]["PREFIX_NAME"].ToString(), dt.Rows[i]["NAME_PREFIX_SEQ"].ToString()));
            }
            e.Message = comboboxUtilites.GetStatusMessage(itemOffset, dt.Rows.Count);
        }

        protected void cmbEduDegree_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            
            cmbEduDegree.Items.Clear();
            
            DataSet ds = null;
            DataTable dt = null;

            ds = degreeBLL.LoadEducationDegree((int?)null);

           

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            //int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEduDegree.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_DEGREE_NAME"].ToString(), dt.Rows[i]["EDU_DEGREE_SEQ"].ToString()));
            }
            e.Message = comboboxUtilites.GetStatusMessage(itemOffset, dt.Rows.Count);

        }

        public object DataItem { get; set; }

        protected void cmbInstitutes_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbInstitutes.Items.Clear();
            bool bLocal = rdoInsLocal.SelectedIndex == 0;

            DataSet ds = null;
            DataTable dt = null;

            ds = insBLL.LoadEducationInstitutes((int?)null, bLocal, e.Text);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbInstitutes.Items.Add(new RadComboBoxItem(dt.Rows[i]["EDU_INS_NAME"].ToString(), dt.Rows[i]["EDU_INS_SEQ"].ToString()));
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void cmbEduDegree_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblDegreeSeq.Text = cmbEduDegree.SelectedValue;
        }
    }
}